package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesChannelRankReportQueryRepository;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesChannelRankReportLoadListRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesChannelRankReportLoadListVO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryMonthSalesReportRequest;
import com.epoch.app.otsapplicationreportsdk.monthsaleschannelrankreport.model.MonthSalesChannelRankReport;
import com.epoch.app.otsapplicationreportsdk.monthsaleschannelrankreport.service.MonthSalesChannelRankReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/12/2 19:33
 */
@Component
public class MonthSalesChannelRankReportQueryRepositoryImpl implements MonthSalesChannelRankReportQueryRepository {

    @Resource
    MonthSalesChannelRankReportService monthSalesChannelRankReportService;

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryList(QueryMonthSalesReportRequest request) {
        MonthSalesChannelRankReportLoadListVO monthSalesChannelRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesChannelRankReport>> list = monthSalesChannelRankReportService.loadListForPage(monthSalesChannelRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryMonthSalesChannelBankReportList(QueryMonthSalesReportRequest request) {
        MonthSalesChannelRankReportLoadListVO monthSalesChannelRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesChannelRankReport>> list = monthSalesChannelRankReportService.loadListForPageSort(monthSalesChannelRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public List<MonthSalesBasicReportSDO> getChannelBankReportList(QueryMonthSalesReportRequest request) {
        MonthSalesChannelRankReportLoadListRequest queryRequest = getListRequestConverter(request);
        Result<List<MonthSalesChannelRankReport>> list = monthSalesChannelRankReportService.loadList(queryRequest);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalQuery(QueryMonthSalesReportRequest request) {
        MonthSalesChannelRankReportLoadListVO monthSalesChannelRankReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesChannelRankReport>> response = monthSalesChannelRankReportService.statisticalQuery(monthSalesChannelRankReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    private MonthSalesChannelRankReportLoadListVO loadListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesChannelRankReportLoadListVO listVO = MonthSalesChannelRankReportLoadListVO.newMonthSalesChannelRankReportLoadListVOBuilder()
                .orgBusinessUnitIds(request.getOrgBusinessUnitIds())
                .orgSalesOrganizationIds(request.getOrgSalesOrganizationIds())
                .orgSalesChannelIds(request.getOrgSalesChannelIds())
                .orgSalesChannelName(request.getOrgSalesChannelName())
                .channelManageEmployeeIds(request.getChannelManageEmployeeIds())
                .statisticalDate(request.getStatisticalDate())
                .dataMark(request.getDataMark())
                .build();
        listVO.setDir(request.getDir());
        listVO.setSortType(request.getSortType());
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        return listVO;
    }

    private MonthSalesChannelRankReportLoadListRequest getListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesChannelRankReportLoadListRequest listRequest = MonthSalesChannelRankReportLoadListRequest.newMonthSalesChannelRankReportLoadListRequestBuilder()
                .orgBusinessUnitId(request.getOrgBusinessUnitId())
                .orgSalesOrganizationId(request.getOrgSalesOrganizationId())
                .orgSalesChannelId(request.getOrgSalesChannelId())
                .planTime(request.getPlanMonth())
                .statisticalDate(request.getStatisticalDate())
                .build();
        return listRequest;
    }

}
